<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/17
 * Time: 12:05
 */

namespace Admin\Controller;


class DocController extends BaseController
{

	public function add()
	{
		if (IS_POST) {
//			编辑文档类型的文件
			$data = new \Org\Util\Ueditor();
//			echo $data->output();
			$model = D('Doc');
			$post = I('post.');
			$file = $_FILES['file'];
//			dump($file);die;
			$result = $model->create();
			if ($result) {
				$result = $model->addData($post, $file);

				if ($result) {
					$this->success('添加成功', 'showList', 3);
				} else {
					$this->error('添加失败');
				}
			}

		} else {
			$this->display();
		}
	}

	public function showList()
	{
		$model = M('Doc');
		$result = $model->select();
//		dump($result);
//		die();
		$this->assign('result', $result);
		$this->display();
	}

	public function delete()
	{
		$get = I('get.');
//		dump($get);die;
		$model = D('Doc');
		$result = $model->delete($get['id']);
//		dump($result);die;
		if ($result) {
			$this->success('删除成功', U('showList', 3));
		} else {
			$this->error('删除失败');
		}
	}

	//showContent方法
	public function showContent()
	{
		//接收id
		$id = I('get.id');
		//查询数据
		$data = M('Doc')->find($id);
		//输出内容，并且还原被转移的字符
		echo htmlspecialchars_decode($data['content']);

	}

	public function download()
	{
//		•$file = "D:/Uploads/photo.jpg";
//•header("Content-type: application/octet-stream");
//•header('Content-Disposition: attachment; filename="' . basename($file) . '"');
//•header("Content-Length: ". filesize($file));
//•readfile($file);
		$id = I('get.id');
		$model = M('Doc');
//		dump($id);die;
		$result = $model->find($id);
		$file = WORKING_PATH . $result['filepath'];
//		dump($file);die;
		header("Content-type: application/octet-stream");
		header('Content-Disposition: attachment; filename="' . basename($file) . '"');
		header("Content-Length: " . filesize($file));
		readfile($file);
	}

	public function edit()
	{
		if (IS_POST) {
			//			编辑文档类型的文件
			$data = new \Org\Util\Ueditor();
//			echo $data->output();
			$model = D('Doc');
			$post = I('post.');
			$file = $_FILES['file'];
//			dump($file);die;
			$result = $model->create();
			if ($result) {
				$result = $model->edit($post, $file);

				if ($result) {
					$this->success('编辑成功',  U('showList'), 3);
				} else {
					$this->error('编辑失败');
				}
			}
		} else {
			$id = I('get.id');
			$model = D('Doc');
//			dump($id) ;die;
			$result = $model->find($id);
			$this->assign('result', $result);
			$this->display();
		}

	}
}
